package asdf.trial.activemq;

import java.io.ByteArrayOutputStream;

import javax.jms.BytesMessage;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class P2PTest
{

    public static void main(String[] args) throws Exception
    {
        final String queue_name = "test.p2p";
        ConnectionFactory connFactory = new ActiveMQConnectionFactory(
                "ethankserver", "serverethank", "tcp://123.57.231.228:61616");
        Connection conn = connFactory.createConnection();
        conn.start();

        final Session session = conn.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);

        Destination dest = session.createQueue(queue_name);

        MessageConsumer consumer = session.createConsumer(dest);
        consumer.setMessageListener(new MessageListener()
        {
            @Override
            public void onMessage(Message message)
            {
                try {
                    if ( message instanceof BytesMessage ) {
                        BytesMessage bm = (BytesMessage) message;
                        System.out.println("bytes <== " + bm);
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        byte[] b = new byte[1024];
                        int c = bm.readBytes(b);
                        while ( c > 0 ) {
                            baos.write(b, 0, c);
                            c = bm.readBytes(b);
                        }
                        System.out.println("bytetest ==> " + baos.toString());
                    } else if ( message instanceof TextMessage ) {
                        TextMessage tm = (TextMessage) message;
                        System.out.println("text <== " + tm.getText());
                    } else {
                        System.out.println("<== " + message);
                    }
                    System.out.println("<== " + message);
                } catch ( Exception e ) {
                    e.printStackTrace();
                }
            }

        });

        final MessageProducer producer = session.createProducer(dest);
        Thread t = new Thread()
        {

            @Override
            public void run()
            {
                int n = 3;
                while ( n-- > 0 ) {
                    try {
                        TextMessage msg =
                                session.createTextMessage("@@@ " + System.currentTimeMillis());
                        msg.setIntProperty("vvv", n);
                        System.out.println("==> " + msg.getText());
                        producer.send(msg);
                        Thread.sleep(1000);
                    } catch ( Exception e ) {
                        e.printStackTrace();
                    }
                }
            }

        };
        //		t.start();

        Thread.sleep(60 * 1000);

    }

}
